package com.polaris.live.ui.live.widget.adapter

import com.blankj.utilcode.util.SpanUtils
import com.polaris.live.adapter.live.LiveMessageAdapter
import com.polaris.live.common.manager.CoroutineScopeManager
import com.polaris.live.databinding.LiveMessageTipsItemBinding
import com.polaris.live.im.sfs.bean.live.SendLiveMessage
import com.polaris.live.ui.message.widget.single_message.AbstractLiveMessageItemHolder
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.utils.viewscope.autoDisposeScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 * LiveMessageTipsItem
 *
 * @author Created by 半仙 on 2023/9/13/013 20:59
 */
class LiveMessageTipsItem(
    adapter: LiveMessageAdapter,
    binding: LiveMessageTipsItemBinding
) : AbstractLiveMessageItemHolder<LiveMessageTipsItemBinding>(adapter, binding) {

    override fun bind(item: SendLiveMessage) {
        if (item.content.contains(SPLIT)) { //有图片
            mBinding.liveMessageTips.autoDisposeScope.launch {
                try {
                    val span = spanStr(item.content)
                    withContext(CoroutineScopeManager.mainDispatcher) {
                        mBinding.liveMessageTips.text = span.create()
                    }
                } catch (_: Exception) {
                }
            }
        } else {
            mBinding.liveMessageTips.text = item.content
        }
    }

    private suspend fun spanStr(input: String): SpanUtils {
        val span = SpanUtils()
        val regex = Regex("$SPLIT(.*?)$SPLIT")
        val matches = regex.findAll(input)
        //标签位置
        val tagPairsPositions = mutableListOf<IntRange>()
        //标签内数据
        val data = matches.map { it.groupValues[1] }.toList()
        for (match in matches) {
            val start = match.range.first
            val end = match.range.last + 1
            tagPairsPositions.add(start until end)
        }
        tagPairsPositions.forEachIndexed { index, intRange ->
            if (index == 0) {
                span.append(input.substring(0, intRange.first))
                addImage(span, data[index])
            } else {
                val last = tagPairsPositions[index - 1]
                span.append(input.substring(last.last + 1, intRange.first))
                addImage(span, data[index])
            }
            if (index == tagPairsPositions.size - 1) {
                span.append(input.substring(intRange.last + 1))
            }
        }
        return span
    }

    private suspend fun addImage(span: SpanUtils, images: String) {
        if (images.contains(IMAGEAND)) {
            val imageUrl = images.replace(IMAGEAND, "")
            val image = ImageUtils.loadNetImageDrawable(
                mBinding.root.context,
                ImageUtils.imgPath(imageUrl),
                16, 16
            )
            if (image != null) {
                span.appendImage(image, SpanUtils.ALIGN_CENTER)
            }
        } else {
            span.append(images)
        }
    }

    companion object {

        const val SPLIT = "<#>"
        const val IMAGEAND = "image="
    }
}